#include "axissprite.h"
#include "axistiledmapscene.h"
#include <QDebug>

AxisSprite::AxisSprite()
{

}

AxisSprite::AxisSprite(int widthgrid, int heightgrid, int wgridsize, int hgridsize)
{
    setSize(widthgrid, heightgrid, wgridsize, hgridsize);
}

void AxisSprite::setSize(int widthgrid, int heightgrid, int wgridsize, int hgridsize)
{
    _map.initMap(widthgrid, heightgrid, wgridsize, hgridsize);
}

void AxisSprite::setScene(AxisTiledMapScene *scene)
{
    _scene = scene;
}

void AxisSprite::updatePos(int gridx, int gridy)
{
    _gridx = gridx;
    _gridy = gridy;

    int offsetx = _map.getMapWidth() / 2;
    int offsety = _map.getMapHeight()/ 2;
    const int originx =  gridx - offsetx;
    const int originy = gridy - offsety;
    for ( int r = 0; r < _map.getMapHeight(); ++r )
    {
        for (int c = 0; c < _map.getMapWidth(); ++c )
        {
            _area.append(QPoint(originx + c, originy + r));
        }
    }

    QPointF topleft = _scene->tileToScreenCoords(originx, originy);
    const qreal wlen = ( _map.getMapHeight() / 2.0f * _map.getGridWidth() ) +
            ( _map.getMapWidth() / 2.0f * _map.getGridWidth() );
    const qreal hlen = ( _map.getMapHeight() / 2.0f * _map.getGridHeight() ) +
            ( _map.getMapWidth() / 2.0f * _map.getGridHeight() );
    const int realox = topleft.x() - ( _map.getMapHeight() / 2.0f * _map.getGridWidth() );
    const int realoy = topleft.y();
    _rect = QRect(realox, realoy, wlen, hlen);
}

QList<QPoint> AxisSprite::getArea() const
{
    return _area;
}

void AxisSprite::choose(bool b)
{
    _showselectcircle = b;
}

QRectF AxisSprite::boundingRect() const
{
    return _rect;
}

void AxisSprite::paint(QPainter *painter)
{
    painter->setRenderHint(QPainter::Antialiasing, true);
    if ( _showselectcircle )
    {
        QPen pen;
        pen.setColor(Qt::green);
        pen.setWidth(5);
        painter->setPen(pen);
        painter->setBrush(Qt::NoBrush);
        //painter->drawRect(_rect);
        painter->drawEllipse(_rect);
    }

    if ( _showblock )
    {
        QBrush brush;
        brush.setColor(Qt::red);
        brush.setStyle(Qt::Dense5Pattern);
        painter->setPen(Qt::NoPen);
        painter->setBrush(brush);
        for( QPoint g : _area)
        {
            float x, y;
            _scene->getMap()->tileToScreenCoords(g.x(), g.y(), x, y);
            QPolygonF polygon = calPolygonPath(x, y);
            painter->drawPolygon(polygon);
        }
    }

    if ( _showaxis )
    {
        QPen pen;
        pen.setWidth(2);
        float originx = 0;
        float originy = 0;
        int gridwidth = _scene->getMap()->getGridWidth();
        int gridheight= _scene->getMap()->getGridHeight();

        _scene->getMap()->tileToScreenCoords(_gridx, _gridy, originx, originy);

        pen.setColor(Qt::blue);
        painter->setPen(pen);
        painter->drawLine(originx, originy, originx - gridwidth, originy + gridheight);

        pen.setColor(Qt::green);
        painter->setPen(pen);
        painter->drawLine(originx, originy, originx + gridwidth, originy + gridheight);

        pen.setColor(Qt::red);
        painter->setPen(pen);
        painter->drawLine(originx, originy, originx, originy - gridwidth);
    }
}

QPolygonF AxisSprite::calPolygonPath(int x, int y)
{
    QPolygonF pointlist;
    pointlist.append(QPoint(x, y));
    pointlist.append(QPoint(x - _map.getGridWidth() / 2, y + _map.getGridHeight() / 2));
    pointlist.append(QPoint(x, y + _map.getGridHeight()));
    pointlist.append(QPoint(x + _map.getGridWidth() / 2, y + _map.getGridHeight() / 2));

    return pointlist;
}
